﻿using System;
using System.IO;
using QuickTestRunResultOptions = QuickTest.RunResultsOptions;
using QuickTestTest = QuickTest.Test;

namespace TestExtensions
{
    public class Test : IDisposable
    {
        #region Variables

        private bool _disposed;
        private QuickTestTest _test;

        #endregion

        #region Constructors

        internal Test(QuickTestTest test, string testPath)
        {
            _disposed = false;
            _test = test;
            TestPath = testPath;
        }

        ~Test()
        {
            Dispose(false);
        }

        #endregion

        #region Properties

        public string TestPath { get; private set; }

        public TestResults Results { get; private set; }

        #endregion

        #region Methods

        public TestResults Run()
        {
            var timeStamp = DateTime.Now.ToTimeStamp();
            var resultsLocation = Path.Combine(TestPath, timeStamp);

            var resultOptions = new QuickTestRunResultOptions();
            resultOptions.ResultsLocation = resultsLocation;
            _test.Run(resultOptions);

            Results = new TestResults(TestPath, timeStamp);
            return Results;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // Dispose of all managed resources.
                }

                // Dispose of all nonmanaged resources:
                _test.Close();
                _test = null;

                _disposed = true;
            }
        }

        #endregion
    }
}